package com.example.config;

import com.alibaba.cola.statemachine.StateMachine;
import com.alibaba.cola.statemachine.builder.StateMachineBuilder;
import com.alibaba.cola.statemachine.builder.StateMachineBuilderFactory;
import com.example.actions.SysSmsCommitAction;
import com.example.actions.SysSmsSuccessAction;
import com.example.context.Context;
import com.example.enums.Events;
import com.example.enums.Status;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SysSmsStateMachineConfig {


    public static final String ID = "sms";

    /****
     *
     * 定义状态机
     * @return
     */
    @Bean
    public StateMachine<Status, Events, Context> stateMachine() {
        StateMachineBuilder<Status, Events, Context> stateMachineBuilder = StateMachineBuilderFactory.create();
        stateMachineBuilder.externalTransition().from(Status.READY).to(Status.SUCCESS).on(Events.SUCCESSED).perform(new SysSmsSuccessAction());
        stateMachineBuilder.externalTransition().from(Status.SUCCESS).to(Status.COMMIT).on(Events.COMMITED).perform(new SysSmsCommitAction());
        return stateMachineBuilder.build(ID);
    }
}
